package cn.icanci.ladybird.controller;

import cn.icanci.ladybird.common.result.JsonData;
import cn.icanci.ladybird.common.util.cache.RedisCacheKeyManager;
import cn.icanci.ladybird.common.util.cache.RedisUtils;
import cn.icanci.ladybird.pojo.dto.UserIndexTicketDetailsDTO;
import cn.icanci.ladybird.pojo.entity.Carousel;
import cn.icanci.ladybird.pojo.entity.TicketDetails;
import cn.icanci.ladybird.service.CarouselService;
import cn.icanci.ladybird.service.TicketDetailsService;
import cn.icanci.ladybird.service.UserInfoService;
import cn.icanci.ladybird.service.user.UserIndexService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author: icanci
 * @date: Created in 2020/10/2 19:29
 * @classAction: 首页的数据展示使用的Controller
 * - 接口的开发规范
 * - /api/v1/pub/user/index
 * - /api/v2/pri/admin/index
 */
@SuppressWarnings("all")
@Controller
@RequestMapping("/api/v1/pub/user")
public class UserIndexController {

    @Autowired
    private UserIndexService userIndexService;
    @Autowired
    private TicketDetailsService ticketDetailsService;
    @Autowired
    private UserInfoService userInfoService;
    @Autowired
    private CarouselService carouselService;
    @Autowired
    private RedisUtils redisUtils;

    @Deprecated
    @ResponseBody
    @RequestMapping("/index")
    public JsonData getUserIndex() {
        List<UserIndexTicketDetailsDTO> userIndexTicketDetailsDTOS = userIndexService.userIndexTicketDetailsDTOList();
        return JsonData.builderSuccess(userIndexTicketDetailsDTOS);
    }

    @Deprecated
    @RequestMapping("/nojson/index")
    public String getUserIndexNoJson(Model model) {
        // 查询列表
        List<UserIndexTicketDetailsDTO> userIndexTicketDetailsDTOS = userIndexService.userIndexTicketDetailsDTOList();
        model.addAttribute("userIndexTicketDetailsDTOS", userIndexTicketDetailsDTOS);
        // 查询大图
        TicketDetails ticketDetails = ticketDetailsService.queryOneByWeight();
        model.addAttribute("ticketDetails", ticketDetails);
        // 查询头像和id
        // model.addxxx

        // 只是用来跳转视图的  文件夹/文件夹/文件名
        return "user/index";
    }

    @RequestMapping("/json/index")
    public String getUserIndexJson(Model model) {
        // 先从Redis中获取首页数据
        JsonData targetData = (JsonData) redisUtils.get(RedisCacheKeyManager.INDEX_DATA_KEY);
        if (null == targetData) {
            // 查询列表
            List<UserIndexTicketDetailsDTO> userIndexTicketDetailsDTOS = userIndexService.userIndexTicketDetailsDTOList();
            // model.addAttribute("userIndexTicketDetailsDTOS",userIndexTicketDetailsDTOS);
            // 查询轮播图
            List<Carousel> carousels = carouselService.queryManyByWeight(false, 1, 7);
            // 查询大图
            UserIndexTicketDetailsDTO userIndexTicketDetailsDTO = userIndexService.userIndexTicketDetailsDTOBigOne();
            // model.addAttribute("ticketDetails",ticketDetails);
            // 查询头像和id
            // model.addxxx
            ArrayList<Object> list = new ArrayList<>();
            list.add(userIndexTicketDetailsDTO);
            list.add(userIndexTicketDetailsDTOS);
            list.add(carousels);
            JsonData jsonData = JsonData.builderSuccess(list);
            model.addAttribute("jsonData", jsonData);
            // 设置到Redis中
            redisUtils.set(RedisCacheKeyManager.INDEX_DATA_KEY, jsonData, 30, TimeUnit.MINUTES);
        } else {
            model.addAttribute("jsonData", targetData);
        }
        return "user/index";
    }
}
